<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/9/9
 * Time: 10:19
 */

namespace app\admin\controller;


use think\Db;

class LotteryPrize extends Base
{
    public $table = 'lottery_prize';
    public $lottery_id;

    public function __construct()
    {
        parent::__construct();
        $this->lottery_id = input('lottery_id');
    }

    public function index()
    {
        $keyword = input('keyword');
        $type = input('type');
        $lottery = $this->checkLottery();

        $where = [ 'site_id' => $this->site_id, 'lottery_id' => $this->lottery_id ];
        if($keyword)
            $where['name'] = ['like','%'.$keyword.'%'];
        if($type)
            $where['type'] = ['eq',$type];

        $data['list'] = Db::name($this->table)
            ->where($where)
            ->order('id desc')
            ->paginate();
        $data['page'] = $data['list']->render();

        $this->assign('data', $data);
        $this->assign('type',$type);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            return $this->save();
        }
        $this->checkLottery();
        $this->getLotteryDefaultPrize();
        $this->assign('data', 1);
        $this->assign('title', '新增');
        $this->assign('url', url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id', $id)->find();
        if (!$data) {
            $this->error('不存在');
        }
        if (request()->isPost()) {
            return $this->save($data);
        }
        $this->checkLottery();
        $this->getLotteryDefaultPrize();
        $temp = Db::name('lottery_default_prize')->where('id',$data['default_id'])->value('stock');
        $data['total_stock'] = $temp + $data['stock'];
        $this->assign('data', json_encode($data));
        $this->assign('title', '编辑');
        $this->assign('url', url('update'));
        return $this->fetch('info');
    }

    public function ajax_del() {
        $id = input('id/d');
        $lottery = $this->checkLottery();
        if ($lottery['start'] < time() && $lottery['end'] > time()) {
            echo api_return(0, '活动正在进行,禁止删除奖品');
            return;
        }
        $res = Db::name('lottery_prize')->where('id', $id)->delete();
        if ($res) {
            echo api_return(1, '删除成功');
        } else {
            echo api_return(0, '删除失败');
        }
    }

    public function save($prize = [])
    {
        $id = input('id/d');
        $param = input();
        $data = [
            'name' => input('name'),
            'img' => input('img'),
            'stock' => input('stock/d'),
            'chance' => input('chance/d'),
            'enable' => input('enable/d'),
            'modtime' => time(),
        ];
        if ($id) {
            $a = Db::name($this->table)->where('id', $id)->update($data);
            $default = Db::name('lottery_default_prize')->where('id',$param['default_id'])->find();
            if ($param['type'] != 1 && $prize['stock'] != $data['stock']) {
                $model = [
                    'stock' => $default['stock'] + ($prize['stock'] - $data['stock']),
                    'modtime' => time(),
                ];
                Db::name('lottery_default_prize')->where('id', $param['default_id'])->update($model);
            }
        } else {
            $data['addtime'] = time();
            $data['site_id'] = $this->site_id;
            $data['lottery_id'] = $this->lottery_id;
            $data['default_id'] = input('default_id');
            $data['type'] = input('type');
            if(Db::name('lottery_prize')->where('default_id',$data['default_id'])->find()){
                return dataReturn(0,'不能重复添加');
            }
            $a = Db::name($this->table)->insert($data);
            if ($param['type'] != 1) {
                Db::name('lottery_default_prize')->where('id', $data['default_id'])->setDec('stock', $data['stock']);
            }
        }
        if ($a) {
            echo api_return(1, '保存成功');
        } else {
            echo api_return(0, '保存失败');
        }
        return;
    }

    public function ajaxupdate()
    {
        $type = input('type');
        $field = input('field');
        $data[$field] = input('value/d');
        $id = input('id/d');
        $data['modtime'] = time();
        $g = Db::name($this->table)->where('id', $id)->find();
        if (!$g)
            return dataReturn(0, '不存在');
        if ($field == 'extra') {
            $data['total'] = $g['sale'] + $data['extra'];
        }
        $a = Db::name($this->table)->where('id', $id)->update($data);
        if ($a) {
            echo api_return(1, '保存成功');
        } else {
            echo api_return(0, '保存失败');
        }
    }

    public function checkLottery($lottery_id = '')
    {
        if (!$lottery_id)
            $lottery_id = input('lottery_id');
        $lottery = Db::name('lottery')->where('id', $lottery_id)->where('site_id', $this->site_id)->find();
        if (!$lottery)
            return $this->error('活动不存在');
        $this->assign('lottery', $lottery);
        $this->assign('lottery_json', json_encode($lottery, JSON_UNESCAPED_UNICODE));
        return $lottery;
    }

    public function getLotteryDefaultPrize()
    {
        $config['prize'] = Db::name('lottery_default_prize')->where('site_id', $this->site_id)->order('id desc')->select();
        $this->assign('config', json_encode($config, JSON_UNESCAPED_UNICODE));
        return $config;
    }

}